package com.cgbl.service.ui

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.StringUtils
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.databinding.AppActivityLoginPasswordBinding
import com.cgbl.service.vm.LoginPasswordViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.util.ToastUtils
import com.umeng.socialize.UMAuthListener
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.UMShareConfig
import com.umeng.socialize.bean.SHARE_MEDIA

@Route(path = AppConstants.Router.Login.A_LOGIN_PASSWORD)
class LoginPasswordActivity :
    GLBaseActivity<AppActivityLoginPasswordBinding, LoginPasswordViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_login_password

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        addSingleTitleBar("")
        setViewData()
        initLive()
    }

    private fun setViewData() {
//        binding.tvPrompt.text = StringUtils.getString(R.string.app_en_tips_7)
//        SpanUtils.with(binding.tvPrompt).append(StringUtils.getString(R.string.app_en_tips_5))
//            .append(StringUtils.getString(R.string.app_en_tips_6))
////            .setForegroundColor(Color.RED)
//            .setClickSpan(resources.getColor(R.color.app_286FFF), false) {
//                ToastUtils.showShort("点击了用户协议")
//            }.append(StringUtils.getString(R.string.app_en_tips_8))
//            .setForegroundColor(resources.getColor(R.color.app_286FFF))
////            .setQuoteColor(Color.RED)
//            .append(StringUtils.getString(R.string.app_en_tips_9))
//            .setClickSpan(resources.getColor(R.color.app_286FFF), false) {
//                ToastUtils.showShort("点击了隐私协议")
//            }.create()
    }

    private fun initLive() {
        viewModel.toLoginType.observe(this, Observer {
            it ?: return@Observer
            toWx(it)
        })
    }

    private fun toWx(type: SHARE_MEDIA) {
        if (!UMShareAPI.get(this).isInstall(this, type)) {
            ToastUtils.showShort(
                "${
                    when (type) {
                        SHARE_MEDIA.QQ -> {
                            StringUtils.getString(R.string.app_error_11)
                        }
                        SHARE_MEDIA.WEIXIN -> {
                            StringUtils.getString(R.string.app_error_10)
                        }
                        SHARE_MEDIA.SINA -> {
                            StringUtils.getString(R.string.app_error_12)
                        }
                        else -> {
                            ""
                        }
                    }
                }"
            )
            return
        }
        val config = UMShareConfig()
        config.isNeedAuthOnGetUserInfo(true)
        UMShareAPI.get(this).setShareConfig(config)
        UMShareAPI.get(this).getPlatformInfo(this, type, object : UMAuthListener {
                override fun onStart(p0: SHARE_MEDIA?) {
                    LogUtils.e(p0)
                }

                override fun onComplete(
                    p0: SHARE_MEDIA?, p1: Int, p2: MutableMap<String, String>?
                ) {
                    when (type) {
                        SHARE_MEDIA.SINA -> {
                            p2?.get("uid")?.let {
                                LogUtils.e(it)

                            }
                        }
                        SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QQ -> {
                            p2?.get("openid")?.let {

                            }
                        }
                    }
                }

                override fun onError(p0: SHARE_MEDIA?, p1: Int, p2: Throwable?) {
                    LogUtils.e(p0, p1, p2)
                    ToastUtils.showShort(p2?.message ?: "")
                }

                override fun onCancel(p0: SHARE_MEDIA?, p1: Int) {
                    LogUtils.e(p0, p1)
                }
            })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }

}